package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.commons.util.StringUtils;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity.SysDict;
import cn.wgx.modules.sys.services.SysDictService;
import cn.wgx.modules.sys.util.DictUtil;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Api(tags = "7. 部门管理", value = "value",
        protocols = "protocols", produces = "produces", consumes = "consumes")
//@RestController
//@RequestMapping(value="${allPathPre}department")
public class DepartmentController extends BaseController {

    @Resource
    SysDictService sysDictService;

    @ApiOperation(value = "列表",notes = "获取部门列表,做成树形结构, k为主键,parentKey为父键,v为值.")
    @RequestMapping(path = "list", method = RequestMethod.GET)
    @ApiPermission(false)
    public Map<String, Object> getList(@ApiParam("部门名称,模糊查询") @RequestParam(required = false) String v,
                                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                       @ApiParam("选择排序时必填根据哪个字段排序") @RequestParam(required = false) String title,
                                       @ApiParam("desc或asc排序") @RequestParam(required = false) String order) {
        SysDict sysDict = new SysDict();
        sysDict.setType(DictUtil.TYPE_DEPARTMENT);
        if(StringUtils.isNotBlank(v)){
            sysDict.setV(v);
        }
        return returnData(PageHelper.startPage(page, pageSize), sysDictService.getList(sysDict,title,order));
    }


    @ApiOperation(value = "新增", notes = "必须参数: v(部门名称), parentKey(上级部门k,顶级为0). 其他参数可选")
    @RequestMapping(path = "insert", method = RequestMethod.POST)
    @ApiPermission
    public Map<String, Object> insertOne(@RequestBody SysDict sysDict) {
        if(StringUtils.isBlank(sysDict.getV()) || StringUtils.isBlank(sysDict.getParentKey())){
            return returnErrorMap("参数错误");
        } else if(!sysDictService.checkV(sysDict.getV())){
            return returnErrorMap("部门名重复，请重新输入");
        }

        sysDict.setType(DictUtil.TYPE_DEPARTMENT);
        sysDict.setK(UUID.randomUUID().toString().replaceAll("-", ""));
        return returnData(sysDictService.insertOne(sysDict));
    }


    @ApiOperation(value = "更新", notes = "必须参数: dict_id, v.  其他可选.")
    @RequestMapping(path = "update", method = RequestMethod.PATCH)
    @ApiPermission
    public Map<String, Object> updateOne(@RequestBody SysDict sysDict) {
        if(sysDict.getDict_id() == null || StringUtils.isBlank(sysDict.getV())){
            return returnErrorMap("参数错误.");
        }
        SysDict dict = new SysDict();
        dict.setV(sysDict.getV());
        List<SysDict> list = sysDictService.getList(dict,"-1",null);
        for (SysDict dc:list) {
            if(list.size() >= 1 && !dc.getDict_id().equals(sysDict.getDict_id())){
                return returnErrorMap("部门名重复，请重新输入");
            }
        }
        sysDict.setType(DictUtil.TYPE_DEPARTMENT);
        return returnData(sysDictService.updateOne(sysDict));

    }

    @ApiOperation("删除")
    @RequestMapping(path = "del/{id}", method = RequestMethod.DELETE)
    @ApiPermission
    public Map<String, Object> deleteOne(@PathVariable("id") Integer id) {
        return returnData(sysDictService.deleteOne(id));
    }

    @ApiOperation("刷新缓存")
    @RequestMapping(path = {"flush"}, method = RequestMethod.DELETE)
    @ApiPermission(false)
    public Map<String, Object> clearCache(@RequestParam(value = "type", required = false) String type) {
        return returnData(sysDictService.clearCache(type));
    }

}
